package com.fly.system.service.impl;

import com.fly.common.model.SysUserModel;
import com.fly.common.model.ZtreeModel;
import com.fly.common.util.UserlUtil;
import com.fly.system.mapper.SysMenuMapper;
import com.fly.system.mapper.SysRoleMapper;
import com.fly.system.mapper.SysRoleMenuMapper;
import com.fly.system.model.SysMenuModel;
import com.fly.system.model.SysRoleMenuModel;
import com.fly.system.model.SysRoleModel;
import com.fly.system.service.SysMenuService;
import com.fly.system.service.SysRoleService;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @CLASSNAME: SysMenuServiceImpl
 * @author: Fly
 * 日期: 2020/6/13  13:17
 * 描述:角色
 **/

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    @Override
    public List<SysRoleModel> findPage(SysRoleModel model) {
        return sysRoleMapper.selectList(model);
    }

    @Override
    @Transactional
    public int saveModel(SysRoleModel model) {
        UserlUtil.setInsertColumn(model);
        int i = sysRoleMapper.insert(model);
        SysRoleModel one = sysRoleMapper.selectOne(model);
        String menuIds = (String) model.getParam().get("menuId");
        if(StringUtils.isNotBlank(menuIds)){
            SysRoleMenuModel rm = new SysRoleMenuModel();
            UserlUtil.setInsertColumn(rm);
            for (String menuId : menuIds.split(",")) {
                rm.setMenuId(Long.parseLong(menuId));
                rm.setRoleId(one.getId());
                i += sysRoleMenuMapper.insert(rm);
            }
        }
        return i;
    }

    @Override
    public SysRoleModel findModelById(Long id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysRoleMenuModel> findRoleMenuByRoleId(Long id) {
        SysRoleMenuModel model = new SysRoleMenuModel();
        model.setRoleId(id);
        return sysRoleMenuMapper.select(model);
    }

    @Override
    @Transactional
    public int editModelById(SysRoleModel model) {
        UserlUtil.setUpdateColumn(model);
        int i = sysRoleMapper.updateByPrimaryKeySelective(model);
        String menuIds = (String) model.getParam().get("menuId");
        if(StringUtils.isNotBlank(menuIds)){
            SysRoleMenuModel rm = new SysRoleMenuModel();
            rm.setRoleId(model.getId());
            sysRoleMenuMapper.delete(rm);
            UserlUtil.setUpdateColumn(rm);
            UserlUtil.setInsertColumn(rm);
            for (String menuId : menuIds.split(",")) {
                rm.setMenuId(Long.parseLong(menuId));
                i += sysRoleMenuMapper.insert(rm);
            }
        }
        return i;
    }

    @Override
    public int removeModelById(SysRoleModel model) {
        int i = sysRoleMapper.delete(model);
        SysRoleMenuModel rm = new SysRoleMenuModel();
        rm.setRoleId(model.getId());
        List<SysRoleMenuModel> list = sysRoleMenuMapper.selectList(rm);
        for (SysRoleMenuModel r : list){
            i += sysRoleMenuMapper.delete(r);
        }
        return i;
    }

    @Override
    public Set<String> findRoleNameByUserId(Long userId) {
        System.out.println("=================adadadad===========");
        List<String> list = sysRoleMapper.selectRoleNameByUserId(userId);
        Set<String> roles = Sets.newHashSet();
        list.forEach(str->{
            roles.add(str);
        });
        return roles;
    }

    @Override
    public Set<String> findMenuPmsByUserId(Long userId) {
        List<String> list = sysRoleMapper.selectRolePmsByUserId(userId);
        Set<String> pms = Sets.newHashSet();
        list.forEach(str->{
            pms.add(str);
        });
        return pms;
    }
}
